﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using YJKRoomComponentInfo.Forms;
using YJKRoomComponentInfo.RoomRoofModule;
using YArchitech.Controls;
using YArchitech.Revit;
using YJKRevitView;

namespace YJKRoomComponentInfo.Command
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdAttachRoomName : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string messages, ElementSet elements)
		{
			this.doc = cmdData.Application.ActiveUIDocument.Document;
			int num = 0;
			if (Convert.ToInt32(cmdData.Application.Application.VersionNumber) < 2014)
			{
				messages = "此功能只在Revit2014及其以上版本可用。";
				return Autodesk.Revit.UI.Result.Failed;
			}
			CmdViewType types = (CmdViewType)11;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			bool flag = true;
			foreach (Element element in new FilteredElementCollector(this.doc).OfClass(typeof(SpatialElement)).ToElements())
			{
				if (element is Room && (element as Room).Location != null)
				{
					flag = false;
					break;
				}
			}
			if (flag)
			{
				YJKMessageBox.Show("请先布置房间");
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			ComponentSelectForm componentSelectForm = new ComponentSelectForm();
			AddDataToEachType addDataToEachType = new AddDataToEachType(cmdData);
			if (DialogResult.OK == componentSelectForm.ShowDialog())
			{
				foreach (string a in componentSelectForm.m_lstComponentType)
				{
					if (!(a == "WallType"))
					{
						if (!(a == "FloorType"))
						{
							if (!(a == "ColumnType"))
							{
								if (!(a == "CeilingType"))
								{
									if (!(a == "DecorateType"))
									{
										if (a == "OtherType")
										{
											num += addDataToEachType.AddDataToOtherType();
										}
									}
									else
									{
										num += addDataToEachType.AddDataToWallDecorateType();
									}
								}
								else
								{
									num += addDataToEachType.AddDataToFloorOrCeilingType("CeilingType");
								}
							}
							else
							{
								num += addDataToEachType.AddDataToColumnType();
							}
						}
						else
						{
							num += addDataToEachType.AddDataToFloorOrCeilingType("FloorType");
						}
					}
					else
					{
						num += addDataToEachType.AddDataToWallType();
					}
				}
				List<Element> list = new List<Element>();
				Transaction transaction = new Transaction(this.doc, "CreateGroups");
				transaction.Start();
				foreach (KeyValuePair<Group, List<ElementId>> keyValuePair in addDataToEachType.dicEachGroupToEid)
				{
					foreach (ElementId elementId in keyValuePair.Value)
					{
						list.Add(this.doc.GetElement(elementId));
					}
					this.CreateGroup(list);
				}
				transaction.Commit();
				if (num != 0)
				{
					YJKMessageBox.Show("已为" + num.ToString() + "个构件添加房间属性。");
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public void CreateGroup(List<Element> elems)
		{
			ElementSet elementSet = new ElementSet();
			foreach (Element element in elems)
			{
				if (element != null)
				{
					elementSet.Insert(element);
				}
			}
			this.doc.CreatYJKGroup(elementSet);
		}

		private Document doc;
	}
}
